require "ttWxCommon"


LIGHT_GREEN = Colour.new(0,0x7f,0)
LIGHT_RED_GREEN = Colour.new(0x7f,0x7f,0)
GREY = Colour.new(0x7f,0x7f,0x7f)

$globalCLexicalStyle =
{
	STC_C_DEFAULT => BLACK,
	STC_C_COMMENT => LIGHT_GREEN,
	STC_C_COMMENTLINE => LIGHT_GREEN,
	STC_C_COMMENTDOC => LIGHT_GREEN,
	STC_C_NUMBER => RED,
	STC_C_WORD => BLUE,
	STC_C_STRING => GREY,
	STC_C_CHARACTER => RED,
	STC_C_UUID => RED,
	STC_C_PREPROCESSOR => LIGHT_RED_GREEN,
	STC_C_OPERATOR => BLUE,
	STC_C_IDENTIFIER => BLACK,
	STC_C_STRINGEOL => RED,
	STC_C_VERBATIM => RED,
	STC_C_REGEX => LIGHT_GREY,
	STC_C_COMMENTLINEDOC => RED,
	STC_C_WORD2 => BLUE,
	STC_C_COMMENTDOCKEYWORD => LIGHT_GREEN,
	STC_C_COMMENTDOCKEYWORDERROR => LIGHT_GREEN,
	STC_C_GLOBALCLASS => CYAN
}
$globalCKeyWord =
 [
	"and","and_eq","asm","auto","bitand","bitor","bool",
	"break","case","catch","char","class","compl","const",
	"const_cast","continue","default","delete","do","double",
	"dynamic_cast","else","enum","explicit","export","extern",
	"false","float","for","friend","goto","if","inline","int",
	"long","mutable","namespace","new","not","not_eq","operator",
	"or","or_eq","private","protected","public","register",
	"reinterpret_cast","return","short","signed","sizeof","static",
	"static_cast","struct","switch","template","this","throw","true",
	"try","typedef","typeid","typename","union","unsigned","using",
	"virtual","void","volatile","wchar_t","while","xor","xor_eq"
]

class MainFrameEditorNoteBook < AuiNotebook
	def initialize(frm,auiMgr)
		@frm=frm
		@auiMgr=auiMgr
		@sciMgr={}
		@notebook_style=AUI_NB_DEFAULT_STYLE
		super(@frm,ID_ANY,DEFAULT_POSITION,DEFAULT_SIZE,@notebook_style)
		@pi = AuiPaneInfo.new
		@pi.set_name("ProjectTextEditor")
		@pi.center_pane.show
		@auiMgr.add_pane(self, @pi)
	end
	def on_size()
	end
	def openFile(id,path)
#		puts __FILE__,__LINE__,id,path
		if nil == @sciMgr[id] then
			@sciMgr[id] = StyledTextCtrl.new(self)
			add_page(@sciMgr[id],path)
			loadTextStyle(@sciMgr[id],path)
			loadTextFile(@sciMgr[id],path)
		else
			p "File already opened"
		end
	end
	def loadTextStyle(sci,name)
#		p SystemSettings.get_font(SYS_DEFAULT_GUI_FONT)
		font = Font.new(12, DEFAULT, NORMAL, NORMAL)
		sci.style_set_font(STC_STYLE_DEFAULT, font)
#		sci.set_edge_mode(Wx::SCI_EDGE_LINE)
		tmppath = Pathname.new(name)
		type = tmppath.extname()
		if /head|cstd|cpp|cl/ =~ matchFileType(type) then
			loadCppStyle(sci)
		end
	end
	def loadCppStyle(sci)
		sci.set_lexer(STC_LEX_CPP)
		sci.style_clear_all
		$globalCLexicalStyle.each do |key,value|
			sci.style_set_foreground(key, value)
		end
		keyWords=""
		$globalCKeyWord.each do |word|
			keyWords = keyWords + word + " "
		end
		p keyWords
		sci.set_key_words(0,keyWords)
	end
	def loadTextFile(sci,name)
		locaText = ""
		fullPath = Pathname.new(name)
		homePath = nil
		if $globalProjectHome then
			homePath = Pathname.new( $globalProjectHome + name)
		end
		readPath = nil
		if homePath and homePath.exist?() then
			readPath = homePath
		end
		if fullPath and fullPath.exist?() then
			readPath = fullPath
		end
		if readPath and readPath.exist?() then
			File.open(readPath) do |fp| 
				fp.each do |line| 
					locaText = locaText + line
				end
			end
		end
		sci.add_text(locaText)
	end
end
